package com.neulogistics.contrib.procuredcenter.api.application.command.handler;

import com.blackto.lib.mediat.unicast.IRequestHandler;
import com.neulogistics.contrib.procuredcenter.api.application.command.UpdateGoodsListCommand;
import com.neulogistics.contrib.procuredcenter.api.application.command.UpdateGoodsSetCommand;
import com.neulogistics.contrib.procuredcenter.domain.model.aggregate.GoodsList;
import com.neulogistics.contrib.procuredcenter.domain.model.aggregate.GoodsSet;
import com.neulogistics.contrib.procuredcenter.domain.repository.IGoodsListRepository;
import com.neulogistics.contrib.procuredcenter.domain.repository.IGoodsSetRepository;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class UpdateGoodsSetCommandHandler implements IRequestHandler<UpdateGoodsSetCommand, BaseResponse> {
    @Resource
    IGoodsSetRepository goodsSetRepository;

    @Override
    public BaseResponse handle(UpdateGoodsSetCommand command) throws Exception {
        GoodsSet goodsSet = goodsSetRepository.findById(command.setId);

        if(goodsSet == null){
            return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR);
        }

        goodsSet.setSetName(command.setName);
        goodsSet.setSetDescription(command.setDescription);

        goodsSetRepository.save(goodsSet);

        return ResultUtils.success(null);
    }
}
